package com.freewater.javabase.demo.datetime;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;

import static java.time.format.DateTimeFormatter.*;

public class DateTimeFormatterDemo {
    public static void main(String[] args) {
        // 2024-04-25 10:30:30.999
        ZonedDateTime dateTime = ZonedDateTime.of(
                LocalDate.of(2024, 4, 25),
                LocalTime.of(10, 30, 30, 999),
                ZoneId.systemDefault()
        );
        // 格式化
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println(formatter.format(dateTime));
        System.out.println(BASIC_ISO_DATE.format(dateTime));// 20240425+0800
        System.out.println(ISO_LOCAL_DATE.format(dateTime));// 2024-04-25
        System.out.println(ISO_OFFSET_DATE.format(dateTime));// 2024-04-25+08:00
        System.out.println(ISO_DATE.format(dateTime));// 2024-04-25+08:00
        System.out.println(ISO_LOCAL_TIME.format(dateTime));// 10:30:30.000000999
        System.out.println(ISO_OFFSET_TIME.format(dateTime));// 10:30:30.000000999+08:00
        System.out.println(ISO_TIME.format(dateTime));// 10:30:30.000000999+08:00
        System.out.println(ISO_LOCAL_DATE_TIME.format(dateTime));// 2024-04-25T10:30:30.000000999
        System.out.println(ISO_OFFSET_DATE_TIME.format(dateTime));// 2024-04-25T10:30:30.000000999+08:00
        System.out.println(ISO_ZONED_DATE_TIME.format(dateTime));// 2024-04-25T10:30:30.000000999+08:00[Asia/Shanghai]
        System.out.println(ISO_DATE_TIME.format(dateTime));// 2024-04-25T10:30:30.000000999+08:00[Asia/Shanghai]
        System.out.println(ISO_ORDINAL_DATE.format(dateTime));// 2024-116+08:00
        System.out.println(ISO_WEEK_DATE.format(dateTime));// 2024-W17-4+08:00
        System.out.println(ISO_INSTANT.format(dateTime));// 2024-04-25T02:30:30.000000999Z
        System.out.println(RFC_1123_DATE_TIME.format(dateTime));// Thu, 25 Apr 2024 10:30:30 +0800
        // 解析
        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        LocalDate localDate = formatter1.parse("2023/10/10", LocalDate::from);
        System.out.println(localDate);// 2023-10-10

        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        localDate = LocalDate.parse("2023/10/10",formatter2);
        System.out.println(localDate);// 2023-10-10
        // 自定义格式化器
        formatter = new DateTimeFormatterBuilder().appendLiteral("Day is:")
                .appendValue(ChronoField.DAY_OF_MONTH,2)
                .appendLiteral(", month is:")
                .appendValue(ChronoField.MONTH_OF_YEAR,2)
                .appendLiteral(", and year:")
                .appendValue(ChronoField.YEAR,4)
                .appendLiteral(" with the time:")
                .appendValue(ChronoField.HOUR_OF_DAY)
                .appendLiteral(":")
                .appendValue(ChronoField.MINUTE_OF_HOUR)
                .toFormatter();
        String str = dateTime.format(formatter);
        System.out.println(str); // Day is:25, month is:04, and year:2024 with the time:10:30
    }
}
